#include "StdAfx.h"
#include "SupportLang.h"

DWORD Hex2Dec(CString strHex)
{
	DWORD dwDec = 0;
	int i;
	for (i=0;i<strHex.GetLength();i++)
	{
		dwDec<<=4;
		switch (strHex.GetAt(i))
		{
		case _T('0'):			dwDec += 0;			break;
		case _T('1'):			dwDec += 1;			break;
		case _T('2'):			dwDec += 2;			break;
		case _T('3'):			dwDec += 3;			break;
		case _T('4'):			dwDec += 4;			break;
		case _T('5'):			dwDec += 5;			break;
		case _T('6'):			dwDec += 6;			break;
		case _T('7'):			dwDec += 7;			break;
		case _T('8'):			dwDec += 8;			break;
		case _T('9'):			dwDec += 9;			break;
		case _T('a'):
		case _T('A'):			dwDec += 10;			break;
		case _T('b'):
		case _T('B'):			dwDec += 11;			break;
		case _T('c'):
		case _T('C'):			dwDec += 12;			break;
		case _T('d'):
		case _T('D'):			dwDec += 13;			break;
		case _T('e'):
		case _T('E'):			dwDec += 14;			break;
		case _T('f'):
		case _T('F'):			dwDec += 15;			break;
		case _T('x'):
		case _T('X'):			dwDec = 0;				break;

		default:
			return 0xFFFFFFFF;		//error
		}
	}
	return dwDec;
}

CSupportLang::CSupportLang(void)
{
}

CSupportLang::~CSupportLang(void)
{
}

void CSupportLang::LoadLangText(CString strLangFilePath)
{
	m_strLanFilePath = strLangFilePath;
	TRACE(_T("LoadLangText: %s\n"), strLangFilePath);
}

BOOL CSupportLang::IsLangSupported(CString strLangFilePath)
{
	TCHAR tzData[MAX_PATH];
	DWORD dwRet;
	ZeroMemory(tzData, MAX_PATH*sizeof(TCHAR));
	dwRet = GetPrivateProfileString(APPNAME_COMMON, KEYNAME_APPNAME, NULL, tzData, MAX_PATH, strLangFilePath);
	if (dwRet>0)
		return TRUE;
	return FALSE;
}

CString CSupportLang::GetString(CString strAppName, CString strKeyName, CString strDefalutString)
{
	if (m_strLanFilePath.GetLength()<1)
		return _T("");

	CString strRet;
	TCHAR tzData[MAX_PATH];
	DWORD dwRet;
	ZeroMemory(tzData, MAX_PATH*sizeof(TCHAR));
	dwRet = GetPrivateProfileString(strAppName, strKeyName, NULL, tzData, MAX_PATH, m_strLanFilePath);
	if (dwRet == 0)
	{
		WritePrivateProfileString(strAppName, strKeyName, strDefalutString, m_strLanFilePath);
		strRet = strDefalutString;
	}
	else
	{
		strRet.Format(_T("%s"), tzData);
	}
	return strRet;
}

LANGID CSupportLang::GetLanguageID(CString &strLangName, CString strLangFilePath)
{
	TCHAR tzData[MAX_PATH];
	DWORD dwRet;
	ZeroMemory(tzData, sizeof(tzData));
	dwRet = GetPrivateProfileString(APPNAME_LANG, KEYNAME_LANG_NAME, NULL, tzData, MAX_PATH, strLangFilePath);
	if (dwRet == 0)
	{
		strLangName = _T("Unknown");
	}
	else
	{
		strLangName.Format(_T("%s"), tzData);
	}

	LANGID langID;
	CString strHex;
	ZeroMemory(tzData, sizeof(tzData));
	dwRet = GetPrivateProfileString(APPNAME_LANG, KEYNAME_LANG_ID, NULL, tzData, MAX_PATH, strLangFilePath);
	if (dwRet == 0)
	{
		langID = 0x0000;
	}
	else
	{
		strHex.Format(_T("%s"), tzData);
		langID = (LANGID)Hex2Dec(strHex);
	}
	return langID;
}
